/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * ProductDataExportor.java
 * business
 * 2012-11-13下午4:40:18
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.data;

import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Product;
import org.jackysoft.service.ProductService;

import com.google.common.collect.Maps;



public class ProductDataExportor implements DataExporter {

	static final Log logger  =  LogFactory.getLog(ProductDataExportor.class);
	private ProductService service;
    

	public ProductDataExportor(ProductService service) {
		super();
		this.service = service;
	}

	@Override
	public byte[] toExcel() throws Exception {
		Collection<Product> pdts = service.gets();
		HSSFWorkbook workbook = new HSSFWorkbook();
		Map<String, Integer> rowIndex = Maps.newHashMap();
		for (Product pdt : pdts) {
			Collection<Department> depts = pdt.getProviders();
			//logger.info("产品关联单位:"+depts);
			for (Department dept : depts) {
				Sheet sheet = workbook.getSheet(dept.getName());
				if (sheet == null) {
					sheet = workbook.createSheet(dept.getName());
				}
				sheet.autoSizeColumn((short) 0);
				sheet.autoSizeColumn((short) 1);
				sheet.autoSizeColumn((short) 2);
				sheet.autoSizeColumn((short) 3);
				Integer rindex = rowIndex.get(dept.getName());
				if (rindex == null) {
					rowIndex.put(dept.getName(), 0);
					rindex = 0;
				} else {
					rowIndex.put(dept.getName(), rindex+1);
				}
				logger.info("部门 "+dept.getName()+",处理到第 "+rindex+" 行");
				Row row = sheet.createRow(rindex);
			    Cell code = row.createCell(0, Cell.CELL_TYPE_STRING);
			    code.setCellValue(pdt.getCode());
			    Cell name = row.createCell(1,Cell.CELL_TYPE_STRING);
			    name.setCellValue(pdt.getName());
			    Cell yval = row.createCell(2, Cell.CELL_TYPE_NUMERIC);
			    yval.setCellValue(0);
			    Cell mval = row.createCell(3, Cell.CELL_TYPE_NUMERIC);
			    mval.setCellValue(0);
			    //logger.info("为 "+dept.getName()+"增加产品:"+pdt.getCode());
			}
			logger.info("产品 "+pdt.getCode()+"处理完毕");
			
		}
		
		ByteArrayOutputStream ous = new ByteArrayOutputStream();
		workbook.write(ous);
		return ous.toByteArray();
	}

}
